#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@IDE     ：PyCharm 
@Author  ：kiway
@Date    ：2024/11/6 22:04 
"""

import multiprocessing
import time
import traceback
import asyncio
from cmrp import get_future_symbol_ls, get_spot_symbol_ls

import ccxt.pro
from cmrp.config import config as _config

config = {"id": "binance",
          # "apiKey": "9e9195bfee82d6670ffbc78c8a5f30b858f79cb9fcffb720222597318b7c5156",
          # "secret": "5a61842e27a3d04a1b4f637d237854eccd82a63f5f8df0671f778b769ac1b87a",
          "options": {'defaultType': 'future'}
          }

exchange = getattr(ccxt.pro, config['id'])({
    'options': config['options'],
    # 'apiKey': config["apiKey"],
    # 'secret': config["secret"],
})

exchange.set_sandbox_mode(_config.sandbox_mode)


def consume_msg(msg_q):
    print("消费进程启动...")
    while True:
        msg = msg_q.get()

        print(f"时间是{int(time.time() * 1000)}，  消息体{msg}")


async def gen_msg(msq_q):
    print("生产进程启动...")
    symbol = get_future_symbol_ls()

    while True:
        try:
            ticks = await exchange.watch_ohlcv_for_symbols([[s, "1m"] for s in symbol])
            msq_q.put(ticks)

        except:
            traceback.print_exc()


if __name__ == '__main__':
    msg_q = multiprocessing.Queue()

    p = multiprocessing.Process(target=consume_msg, args=(msg_q,))
    p.start()

    loop = asyncio.new_event_loop()
    loop.run_until_complete(gen_msg(msg_q))
